<?php

namespace addon\hotel\admin\controller;

use app\admin\controller\BaseAdmin;
use addon\hotel\model\Room as RoomModel;
use app\model\hotel\Hotel as HotelModel;

class Room extends BaseAdmin
{
    public function lists()
    {

        $room_model = new RoomModel();
        if (request()->isAjax()) {

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);

            $alias = 'a';

            $join = [
                [
                    'hotel h',
                    'a.hotel_id = h.hotel_id',
                    'inner'
                ]
            ];

            $condition = [];
            $room_name = input('room_name', '');
            if ($room_name) {
                $condition[] = [ 'a.roomNo', 'like', '%' . $room_name . '%' ];
            }

            $hotel_id = input('hotel_id','');
            if ($hotel_id) {
                $condition[] = [ 'a.hotel_id', '=', $hotel_id ];
            }

            $status = input('status', '');
            if ($status) {
                if ($status==1){
                    $condition[] = [ 'a.status', '=', 1 ];
                } elseif ($status==2){
                    $condition[] = [ 'a.status', '=', 0 ];
                }

            }

            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = [ "a.create_time", ">=", strtotime($start_time) ];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = [ "a.create_time", "<=", strtotime($end_time) ];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = [ "a.create_time", "between", [ strtotime($start_time), strtotime($end_time) ] ];
            }

            $order = 'a.sort desc';

            $field = 'a.*,h.hotel_name,h.direct';

            $list = $room_model->getRoomPageList($condition, $page, $page_size,$order,$field,$alias,$join);
            return $list;
        } else {

            $hotel_model = new HotelModel();

            $hotel_list = $hotel_model->getHotelList([],'hotel_id,hotel_name');

            $this->assign('hotel_list',$hotel_list['data']);
            return $this->fetch('room/lists');
        }

    }

    public function delete()
    {
        if (request()->isAjax()) {
            $room_ids = input('room_id', 0);
            $room_model = new \addon\hotel\model\Room();
            return $room_model->deleteRoom([ [ 'room_id', '=', $room_ids ] ]);
        }
    }

    public function operationRoom()
    {
        if (request()->isAjax()) {
            $room_id = input('room_id', 0);

            $status = input('status', 0);

            $room_model = new \addon\hotel\model\Room();
            return $room_model->editRoom(['status'=>$status],[ [ 'room_id', '=', $room_id ] ]);
        }
    }
}